home *** CD-ROM | disk | FTP | other *** search
- Path: csun.edu!kc44097
- From: kc44097@csun.edu (chen)
- Newsgroups: comp.lang.c
- Subject: Union type
- Date: 10 Apr 1996 20:01:51 GMT
- Organization: California State University, Northridge
- Message-ID: <4kh43f$h4f@dewey.csun.edu>
- NNTP-Posting-Host: louie.csun.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
-
- I have a question about union,can anyonegive me some advice?
- Please e-mail me
- kc44097@huey.csun.edu
- Thankx
- -------------------------------------------------------------
-
- #include <stdio.h>
-
- union {
- int integer;
- float floating;
- } myUnion;
-
-
- main()
- {
-
- myUnion.integer = 1;
-
- printf("The value of integer is %d\n", myUnion.integer);
- printf("The value of floating is %f\n\n", myUnion.floating);
- printf("The value of \"cast\" is %f\n", (float) myUnion.integer);
- printf("The value of \"magic\" is %f\n\n", myUnion.integer);
- printf("The meaning of life ``%d''\n", (int) myUnion.floating);
- return (0);
- }
-
- The optput is :
-
- The value of integer is 1
- The value of floating is 0.000000
- The value of cast is 1.000000
- The value of magic is 0.000000
- The meaning of life ``0''
-
-
- My question is : Why myUnion.integer is 1 but myUnion.floating is
- 0.000000;Why myUnion.integer change to 0.000000 in 4th printf(),
- and why myUnion.floating is 0 in 5th printf()?
-
-